package nix.mall.service.Impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import nix.mall.common.pojo.EasyUIDataGridResult;
import nix.mall.common.pojo.NixMallResult;
import nix.mall.jedis.JedisClient;
import nix.mall.mapper.ItemDescMapper;
import nix.mall.mapper.ItemMapper;
import nix.mall.pojo.Item;
import nix.mall.pojo.ItemDesc;
import nix.mall.pojo.ItemExample;
import nix.mall.service.ItemService;
import nix.mall.utils.IDUtils;
import nix.mall.utils.JsonUtils;

/**
 * 商品管理Service
 */
@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private ItemDescMapper itemDescMapper;
	@Autowired
	private JmsTemplate jsmTemplate;
	@Resource(name="itemAddTopic")
	private Destination destination;
	@Autowired
	private JedisClient jedisClient;
	@Value("${ITEM_INFO}")
	private String ITEM_INFO;
	@Value("${ITEM_EXPIRE}")
	private Integer ITEM_EXPIRE;
	
	@Override
	public Item getItemById(long itemId) {
		// 先查询缓存
		try {
			String json = jedisClient.get(ITEM_INFO + ":" + itemId + ":BASE");
			if (StringUtils.isNotBlank(json)) {
				// json转换为pojo
				Item _item = JsonUtils.jsonToPojo(json, Item.class);
				return _item;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 没有命中缓存，查询数据库
		Item item = itemMapper.selectByPrimaryKey(itemId);
		// 查询结果添加至缓存
		try {
			jedisClient.set(ITEM_INFO + ":" + itemId + ":BASE", JsonUtils.objectToJson(item));
			// 设置过期时间，提高缓存利用率
			jedisClient.expire(ITEM_INFO + ":" + itemId + ":BASE", ITEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return item;
	}

	@Override
	public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
		// 分页信息
		PageHelper.startPage(page, rows);
		// 执行查询
		ItemExample example = new ItemExample();
		List<Item> list = itemMapper.selectByExample(example);
		// 取查询结果
		PageInfo<Item> pageInfo = new PageInfo<>(list);
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setRows(list);
		result.setTotal(pageInfo.getTotal());
		// 返回结果		
		return result;
	}

	@Override
	public NixMallResult addItem(Item item, String desc) {
		// 生成商品Id
		final long itemId = IDUtils.genItemId();
		// 补全Item属性
		item.setId(itemId);
		// 商品状态：1 正常；2 下架；3 删除；
		item.setStatus((byte) 1);
		item.setCreated(new Date());
		item.setUpdated(new Date());
		// 向商品表插入数据
		itemMapper.insert(item);
		// 创建商品描述表pojo
		ItemDesc itemDesc = new ItemDesc();
		// 补全属性
		itemDesc.setItemId(itemId);
		itemDesc.setItemDesc(desc);
		itemDesc.setCreated(new Date());
		itemDesc.setUpdated(new Date());
		// 插入数据
		itemDescMapper.insert(itemDesc);
		// 向ActiveMQ发送商品添加消息
		jsmTemplate.send(destination, new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				// 发送商品Id
				TextMessage textMessage = session.createTextMessage(itemId + "");
				return textMessage;
			}
		});
		// 返回结果
		return NixMallResult.ok();
	}

	@Override
	public ItemDesc getItemDescById(long itemId) {
		// 先查询缓存
		try {
			String json = jedisClient.get(ITEM_INFO + ":" + itemId + ":DESC");
			if (StringUtils.isNotBlank(json)) {
				// json转换为pojo
				ItemDesc _itemDesc = JsonUtils.jsonToPojo(json, ItemDesc.class);
				return _itemDesc;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 没有命中缓存，查询数据库
		ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
		// 查询结果添加至缓存
		try {
			jedisClient.set(ITEM_INFO + ":" + itemId + ":DESC", JsonUtils.objectToJson(itemDesc));
			// 设置过期时间，提高缓存利用率
			jedisClient.expire(ITEM_INFO + ":" + itemId + ":DESC", ITEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return itemDesc;
	}

}
